package com.example.caoh.utils;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.example.caoh.entity.SysUser;


/**
 * @author Ca(OH)2
 *
 * 2018年9月3日
 */
public class SessionUtil {
	

	public static SysUser getUser() {
		if(null==RequestContextHolder.getRequestAttributes())
			return null;
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		HttpSession session = request.getSession();
		if(null==session.getAttribute(Const.SESSION_USER))
			return null;
		return (SysUser) session.getAttribute(Const.SESSION_USER);
	}

	public static void setUser(SysUser user) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		HttpSession session = request.getSession();
		session.setAttribute(Const.SESSION_USER, user);
	}
	
	public static void setMenu(List<Map<String,Object>> menu) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        HttpSession session = request.getSession();
        session.setAttribute(Const.MENU, menu);
    }
	
	
	@SuppressWarnings("unchecked")
    public static List<Map<String,Object>> getMenu() {
	    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        HttpSession session = request.getSession();
        if(null==session.getAttribute(Const.MENU))
            return null;
        return  (List<Map<String, Object>>) session.getAttribute(Const.MENU);
    }
	
//	public static String getEntityCode() {
//		SysUser user = getUser();
//		if(user==null)
//			return null;
//		return user.getEntity_code();
//	}
	
	public static void clearSession() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		HttpSession session = request.getSession();
		session.removeAttribute(Const.SESSION_USER);
	}
	
}
